<?xml version="1.0" encoding="utf-8"?>
<!-- xq537.xsl: converts xq528.xml into xq538.txt -->
<!DOCTYPE stylesheet [
  <!ENTITY space "<xsl:text> </xsl:text>">
  <!ENTITY crlf "<xsl:text>
</xsl:text>">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dbml="http://schemas.microsoft.com/linqtosql/dbml/2007"
>
    <xsl:output method="text" indent="yes"/>
  
  <xsl:template match="dbml:Database">
    <xsl:apply-templates select="dbml:Table/dbml:Type" />
    <xsl:apply-templates select="dbml:Table/dbml:Type/dbml:Association"/>
  </xsl:template>

  <xsl:template match="dbml:Type">
    create table [<xsl:value-of select="@Name"/>] (
    <xsl:for-each select="dbml:Column">
      [<xsl:call-template name="columnName">
        <xsl:with-param name="column" select="."/>
      </xsl:call-template>]&space;
      <xsl:choose>
        <xsl:when test="@DbType != ''">
          <xsl:value-of select="@DbType"/>
        </xsl:when>
        <xsl:otherwise>nvarchar(255) null</xsl:otherwise>
      </xsl:choose>&space;<xsl:if test="position() != last()">,</xsl:if>&crlf;      
    </xsl:for-each>
    <xsl:if test="count(dbml:Column[@IsPrimaryKey='true']) = 1">
      ,primary key(<xsl:call-template name="columnName">
      <xsl:with-param name="column" select="dbml:Column[@IsPrimaryKey='true']"/>
    </xsl:call-template>)
    </xsl:if>)
  </xsl:template>
  <xsl:template match="dbml:Association[@IsForeignKey='true']">
    alter table <xsl:value-of select="../@Name"/> add constraint <xsl:value-of select="@Name"/> foreign key (<xsl:value-of select="@ThisKey"/>) references <xsl:value-of select="@Type"/>
  </xsl:template>
  <xsl:template name="columnName">
    <xsl:param name="column"/>
    <xsl:choose>
      <xsl:when test="$column/@Name != ''">
        <xsl:value-of select="$column/@Name"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$column/@Member"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
